package qihui;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class FSToExcel {

	public static String pathTitle = "C:\\AutomationTest\\v1.4\\v1.4\\openhub fields.txt";
	public static String pathTxt = "C:\\AutomationTest\\v1.4\\v1.4\\1.dat";
	public static String pathExcel = "C:\\AutomationTest\\v1.4\\v1.4\\expected\\openhub.xls";

	public static void main(String args[]) {
		readFileByLines();
	}

	public static void readFileByLines() {
		BufferedReader reader = null;
		BufferedReader readerTitle = null;
		try {
			reader = new BufferedReader(new FileReader(new File(pathTxt)));
			readerTitle = new BufferedReader(new FileReader(new File(pathTitle)));
			WritableWorkbook book = Workbook.createWorkbook(new File(pathExcel));
			String tempString = null;
			int sheetNum = 1;
			WritableSheet sheet = book.createSheet("Sheet " + String.valueOf(sheetNum), 0);
			int col = 0;
			while ((tempString = readerTitle.readLine()) != null) {
				Label label = new Label(col, 0, tempString);
				sheet.addCell(label);
				col++;
			}
			int line = 1;
			while ((tempString = reader.readLine()) != null) {
				byte[] tempByte = tempString.getBytes();
				int length = tempByte.length;
				String strValue = "";
				int column = 0;
				for (int m = 0; m < length; m++) {
					if (tempByte[m] != 28) {
						strValue = strValue + String.valueOf((char) tempByte[m]);
					} else {
						Label label = new Label(column, line, strValue);
						sheet.addCell(label);
						column++;
						strValue = "";
					}
				}
				line++;
				if (line > 60000) {
					line = 1;
					sheetNum++;
					sheet = book.createSheet("Sheet " + String.valueOf(sheetNum), 0);
				}
			}
			book.write();
			book.close();
			reader.close();
			readerTitle.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
			if (readerTitle != null) {
				try {
					readerTitle.close();
				} catch (IOException e1) {
				}
			}
		}
	}
}
